package com.ts.animal.service.impl;

import com.ts.animal.dao.InputsDao;
import com.ts.animal.dao.SickRecordDao;
import com.ts.animal.domain.InputsDO;
import com.ts.animal.domain.SickRecordDO;
import com.ts.animal.service.SickRecordService;
import com.ts.common.utils.Result;
import com.ts.common.utils.ShiroUtils;
import com.ts.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class SickRecordServiceImpl implements SickRecordService {
	@Autowired
	private SickRecordDao sickRecordDao;
	@Autowired
	private InputsDao inputsDao;

	@Override
	public SickRecordDO get(Integer id) {
		return sickRecordDao.get(id);
	}

	@Override
	public List<SickRecordDO> list(Map<String, Object> map) {
		return sickRecordDao.list(map);
	}

	@Override
	public int count(Map<String, Object> map) {
		return sickRecordDao.count(map);
	}

	@Override
	public int save(Result result, SickRecordDO sickRecord) {
		int ret = 0;
		String idListStr = sickRecord.getRemark1();
		sickRecord.setRemark1(null);
		if (StringUtils.isNotEmpty(idListStr)) {
			String[] idList = idListStr.split(",");
			for (String inputId : idList) {
				InputsDO inputsDO = inputsDao.get(Integer.valueOf(inputId));
				if (inputsDO != null) {
					sickRecord.setBatchNum(inputsDO.getBatchNum());
					sickRecord.setDeptId(inputsDO.getDeptId());
					sickRecord.setNum(inputsDO.getNum());
					sickRecord.setTypeName(inputsDO.getTypeName());
					sickRecord.setVarietyName(inputsDO.getVarietyName());
					sickRecord.setInputsId(inputsDO.getId());
					sickRecord.setCreateTime(new Date());
					sickRecord.setCreateUser((int) ShiroUtils.getUserId().longValue());
					ret = +sickRecordDao.save(sickRecord);
				}
			}
		}
		return ret;
	}

	@Override
	public int update(Result result, SickRecordDO sickRecord) {
		int ret = sickRecordDao.update(sickRecord);
		if (ret == 0) {
			result.set(Result.FAIL, "更新失败", null);
		}
		return ret;
	}

	@Override
	public int remove(Result result, Integer id) {
		int ret = sickRecordDao.remove(id);
		if (ret == 0) {
			result.set(Result.FAIL, "删除失败", null);
		}
		return ret;
	}

	@Override
	public int batchRemove(Result result, Integer[] ids) {
		int ret = sickRecordDao.batchRemove(ids);
		if (ret != ids.length) {
			result.set(Result.FAIL, "部分删除失败", null);
		}
		return ret;
	}

}
